

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="python" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="python" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>QUANTAXIS.QAIndicator.base &mdash; QUANTAXIS 1.0.39 documentation</title>
  

  
  
  
  

  

  
  
    

  

  
    <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" /> 

  
  <script src="../../../_static/js/modernizr.min.js"></script>

</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search">
          

          
            <a href="../../../index.html" class="icon icon-home"> QUANTAXIS
          

          
          </a>

          
            
            
              <div class="version">
                1.0.39
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <!-- Local TOC -->
              <div class="local-toc"></div>
            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">QUANTAXIS</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../index.html">Module code</a> &raquo;</li>
        
      <li>QUANTAXIS.QAIndicator.base</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for QUANTAXIS.QAIndicator.base</h1><div class="highlight"><pre>
<span></span><span class="c1"># coding:utf-8</span>
<span class="c1">#</span>
<span class="c1"># The MIT License (MIT)</span>
<span class="c1">#</span>
<span class="c1"># Copyright (c) 2016-2018 yutiansut/QUANTAXIS</span>
<span class="c1">#</span>
<span class="c1"># Permission is hereby granted, free of charge, to any person obtaining a copy</span>
<span class="c1"># of this software and associated documentation files (the &quot;Software&quot;), to deal</span>
<span class="c1"># in the Software without restriction, including without limitation the rights</span>
<span class="c1"># to use, copy, modify, merge, publish, distribute, sublicense, and/or sell</span>
<span class="c1"># copies of the Software, and to permit persons to whom the Software is</span>
<span class="c1"># furnished to do so, subject to the following conditions:</span>
<span class="c1">#</span>
<span class="c1"># The above copyright notice and this permission notice shall be included in all</span>
<span class="c1"># copies or substantial portions of the Software.</span>
<span class="c1">#</span>
<span class="c1"># THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR</span>
<span class="c1"># IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,</span>
<span class="c1"># FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE</span>
<span class="c1"># AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER</span>
<span class="c1"># LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,</span>
<span class="c1"># OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE</span>
<span class="c1"># SOFTWARE.</span>


<span class="kn">from</span> <span class="nn">functools</span> <span class="k">import</span> <span class="n">reduce</span>

<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>


<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">Series 类</span>

<span class="sd">这个是下面以DataFrame为输入的基础函数</span>
<span class="sd">return pd.Series format</span>
<span class="sd">&quot;&quot;&quot;</span>


<div class="viewcode-block" id="EMA"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAIndicator.html#QUANTAXIS.QAIndicator.base.EMA">[docs]</a><span class="k">def</span> <span class="nf">EMA</span><span class="p">(</span><span class="n">Series</span><span class="p">,</span> <span class="n">N</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="o">.</span><span class="n">ewm</span><span class="p">(</span><span class="n">Series</span><span class="p">,</span> <span class="n">span</span><span class="o">=</span><span class="n">N</span><span class="p">,</span> <span class="n">min_periods</span><span class="o">=</span><span class="n">N</span> <span class="o">-</span> <span class="mi">1</span><span class="p">,</span> <span class="n">adjust</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span><span class="o">.</span><span class="n">mean</span><span class="p">()</span></div>


<div class="viewcode-block" id="MA"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAIndicator.html#QUANTAXIS.QAIndicator.base.MA">[docs]</a><span class="k">def</span> <span class="nf">MA</span><span class="p">(</span><span class="n">Series</span><span class="p">,</span> <span class="n">N</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="o">.</span><span class="n">rolling</span><span class="p">(</span><span class="n">Series</span><span class="p">,</span> <span class="n">N</span><span class="p">)</span><span class="o">.</span><span class="n">mean</span><span class="p">()</span></div>

<span class="c1"># 威廉SMA  参考https://www.joinquant.com/post/867</span>


<div class="viewcode-block" id="SMA"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAIndicator.html#QUANTAXIS.QAIndicator.base.SMA">[docs]</a><span class="k">def</span> <span class="nf">SMA</span><span class="p">(</span><span class="n">Series</span><span class="p">,</span> <span class="n">N</span><span class="p">,</span> <span class="n">M</span><span class="o">=</span><span class="mi">1</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    威廉SMA算法</span>

<span class="sd">    本次修正主要是对于返回值的优化,现在的返回值会带上原先输入的索引index</span>
<span class="sd">    2018/5/3</span>
<span class="sd">    @yutiansut</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">ret</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span>
    <span class="n">length</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">Series</span><span class="p">)</span>
    <span class="c1"># 跳过X中前面几个 nan 值</span>
    <span class="k">while</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">length</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">np</span><span class="o">.</span><span class="n">isnan</span><span class="p">(</span><span class="n">Series</span><span class="o">.</span><span class="n">iloc</span><span class="p">[</span><span class="n">i</span><span class="p">]):</span>
            <span class="n">i</span> <span class="o">+=</span> <span class="mi">1</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">break</span>
    <span class="n">preY</span> <span class="o">=</span> <span class="n">Series</span><span class="o">.</span><span class="n">iloc</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>  <span class="c1"># Y&#39;</span>
    <span class="n">ret</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">preY</span><span class="p">)</span>
    <span class="k">while</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">length</span><span class="p">:</span>
        <span class="n">Y</span> <span class="o">=</span> <span class="p">(</span><span class="n">M</span> <span class="o">*</span> <span class="n">Series</span><span class="o">.</span><span class="n">iloc</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">+</span> <span class="p">(</span><span class="n">N</span> <span class="o">-</span> <span class="n">M</span><span class="p">)</span> <span class="o">*</span> <span class="n">preY</span><span class="p">)</span> <span class="o">/</span> <span class="nb">float</span><span class="p">(</span><span class="n">N</span><span class="p">)</span>
        <span class="n">ret</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">Y</span><span class="p">)</span>
        <span class="n">preY</span> <span class="o">=</span> <span class="n">Y</span>
        <span class="n">i</span> <span class="o">+=</span> <span class="mi">1</span>
    <span class="k">return</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">ret</span><span class="p">,</span> <span class="n">index</span><span class="o">=</span><span class="n">Series</span><span class="o">.</span><span class="n">tail</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">ret</span><span class="p">))</span><span class="o">.</span><span class="n">index</span><span class="p">)</span></div>


<div class="viewcode-block" id="DIFF"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAIndicator.html#QUANTAXIS.QAIndicator.base.DIFF">[docs]</a><span class="k">def</span> <span class="nf">DIFF</span><span class="p">(</span><span class="n">Series</span><span class="p">,</span> <span class="n">N</span><span class="o">=</span><span class="mi">1</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">Series</span><span class="p">)</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">N</span><span class="p">)</span></div>


<div class="viewcode-block" id="HHV"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAIndicator.html#QUANTAXIS.QAIndicator.base.HHV">[docs]</a><span class="k">def</span> <span class="nf">HHV</span><span class="p">(</span><span class="n">Series</span><span class="p">,</span> <span class="n">N</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">Series</span><span class="p">)</span><span class="o">.</span><span class="n">rolling</span><span class="p">(</span><span class="n">N</span><span class="p">)</span><span class="o">.</span><span class="n">max</span><span class="p">()</span><span class="o">.</span><span class="n">values</span></div>


<div class="viewcode-block" id="LLV"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAIndicator.html#QUANTAXIS.QAIndicator.base.LLV">[docs]</a><span class="k">def</span> <span class="nf">LLV</span><span class="p">(</span><span class="n">Series</span><span class="p">,</span> <span class="n">N</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">Series</span><span class="p">)</span><span class="o">.</span><span class="n">rolling</span><span class="p">(</span><span class="n">N</span><span class="p">)</span><span class="o">.</span><span class="n">min</span><span class="p">()</span><span class="o">.</span><span class="n">values</span></div>


<div class="viewcode-block" id="SUM"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAIndicator.html#QUANTAXIS.QAIndicator.base.SUM">[docs]</a><span class="k">def</span> <span class="nf">SUM</span><span class="p">(</span><span class="n">Series</span><span class="p">,</span> <span class="n">N</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="o">.</span><span class="n">rolling</span><span class="p">(</span><span class="n">Series</span><span class="p">,</span> <span class="n">N</span><span class="p">)</span><span class="o">.</span><span class="n">sum</span><span class="p">()</span></div>


<div class="viewcode-block" id="ABS"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAIndicator.html#QUANTAXIS.QAIndicator.base.ABS">[docs]</a><span class="k">def</span> <span class="nf">ABS</span><span class="p">(</span><span class="n">Series</span><span class="p">):</span>
    <span class="k">return</span> <span class="nb">abs</span><span class="p">(</span><span class="n">Series</span><span class="p">)</span></div>


<div class="viewcode-block" id="MAX"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAIndicator.html#QUANTAXIS.QAIndicator.base.MAX">[docs]</a><span class="k">def</span> <span class="nf">MAX</span><span class="p">(</span><span class="n">A</span><span class="p">,</span> <span class="n">B</span><span class="p">):</span>
    <span class="n">var</span> <span class="o">=</span> <span class="n">IF</span><span class="p">(</span><span class="n">A</span> <span class="o">&gt;</span> <span class="n">B</span><span class="p">,</span> <span class="n">A</span><span class="p">,</span> <span class="n">B</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">var</span></div>


<div class="viewcode-block" id="MIN"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAIndicator.html#QUANTAXIS.QAIndicator.base.MIN">[docs]</a><span class="k">def</span> <span class="nf">MIN</span><span class="p">(</span><span class="n">A</span><span class="p">,</span> <span class="n">B</span><span class="p">):</span>
    <span class="n">var</span> <span class="o">=</span> <span class="n">IF</span><span class="p">(</span><span class="n">A</span> <span class="o">&lt;</span> <span class="n">B</span><span class="p">,</span> <span class="n">A</span><span class="p">,</span> <span class="n">B</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">var</span></div>


<div class="viewcode-block" id="SINGLE_CROSS"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAIndicator.html#QUANTAXIS.QAIndicator.base.SINGLE_CROSS">[docs]</a><span class="k">def</span> <span class="nf">SINGLE_CROSS</span><span class="p">(</span><span class="n">A</span><span class="p">,</span> <span class="n">B</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">A</span><span class="o">.</span><span class="n">iloc</span><span class="p">[</span><span class="o">-</span><span class="mi">2</span><span class="p">]</span> <span class="o">&lt;</span> <span class="n">B</span><span class="o">.</span><span class="n">iloc</span><span class="p">[</span><span class="o">-</span><span class="mi">2</span><span class="p">]</span> <span class="ow">and</span> <span class="n">A</span><span class="o">.</span><span class="n">iloc</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">&gt;</span> <span class="n">B</span><span class="o">.</span><span class="n">iloc</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]:</span>
        <span class="k">return</span> <span class="kc">True</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">return</span> <span class="kc">False</span></div>


<div class="viewcode-block" id="CROSS"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAIndicator.html#QUANTAXIS.QAIndicator.base.CROSS">[docs]</a><span class="k">def</span> <span class="nf">CROSS</span><span class="p">(</span><span class="n">A</span><span class="p">,</span> <span class="n">B</span><span class="p">):</span>
    <span class="n">var</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">A</span><span class="o">&lt;</span><span class="n">B</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
    <span class="k">return</span> <span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">var</span><span class="p">,</span> <span class="n">index</span><span class="o">=</span><span class="n">A</span><span class="o">.</span><span class="n">index</span><span class="p">)</span><span class="o">.</span><span class="n">diff</span><span class="p">()</span><span class="o">&lt;</span><span class="mi">0</span><span class="p">)</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="nb">int</span><span class="p">)</span></div>


<div class="viewcode-block" id="COUNT"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAIndicator.html#QUANTAXIS.QAIndicator.base.COUNT">[docs]</a><span class="k">def</span> <span class="nf">COUNT</span><span class="p">(</span><span class="n">COND</span><span class="p">,</span> <span class="n">N</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    2018/05/23 修改</span>

<span class="sd">    参考https://github.com/QUANTAXIS/QUANTAXIS/issues/429</span>

<span class="sd">    现在返回的是series</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">COND</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">),</span><span class="n">index</span><span class="o">=</span><span class="n">COND</span><span class="o">.</span><span class="n">index</span><span class="p">)</span><span class="o">.</span><span class="n">rolling</span><span class="p">(</span><span class="n">N</span><span class="p">)</span><span class="o">.</span><span class="n">sum</span><span class="p">()</span></div>

<div class="viewcode-block" id="IF"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAIndicator.html#QUANTAXIS.QAIndicator.base.IF">[docs]</a><span class="k">def</span> <span class="nf">IF</span><span class="p">(</span><span class="n">COND</span><span class="p">,</span> <span class="n">V1</span><span class="p">,</span> <span class="n">V2</span><span class="p">):</span>
    <span class="n">var</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">COND</span><span class="p">,</span> <span class="n">V1</span><span class="p">,</span> <span class="n">V2</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">var</span><span class="p">,</span> <span class="n">index</span><span class="o">=</span><span class="n">V1</span><span class="o">.</span><span class="n">index</span><span class="p">)</span></div>


<div class="viewcode-block" id="REF"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAIndicator.html#QUANTAXIS.QAIndicator.base.REF">[docs]</a><span class="k">def</span> <span class="nf">REF</span><span class="p">(</span><span class="n">Series</span><span class="p">,</span> <span class="n">N</span><span class="p">):</span>
    <span class="n">var</span> <span class="o">=</span> <span class="n">Series</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">N</span><span class="p">)</span>
    <span class="n">var</span> <span class="o">=</span> <span class="n">Series</span> <span class="o">-</span> <span class="n">var</span>
    <span class="k">return</span> <span class="n">var</span></div>


<div class="viewcode-block" id="LAST"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAIndicator.html#QUANTAXIS.QAIndicator.base.LAST">[docs]</a><span class="k">def</span> <span class="nf">LAST</span><span class="p">(</span><span class="n">COND</span><span class="p">,</span> <span class="n">N1</span><span class="p">,</span> <span class="n">N2</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;表达持续性</span>

<span class="sd">    Arguments:</span>
<span class="sd">        COND {[type]} -- [description]</span>
<span class="sd">        N1 {[type]} -- [description]</span>
<span class="sd">        N2 {[type]} -- [description]</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">N2</span> <span class="o">=</span> <span class="mi">1</span> <span class="k">if</span> <span class="n">N2</span> <span class="o">==</span> <span class="mi">0</span> <span class="k">else</span> <span class="n">N2</span>
    <span class="k">assert</span> <span class="n">N2</span> <span class="o">&gt;</span> <span class="mi">0</span>
    <span class="k">assert</span> <span class="n">N1</span> <span class="o">&gt;</span> <span class="n">N2</span>
    <span class="k">return</span> <span class="n">COND</span><span class="o">.</span><span class="n">iloc</span><span class="p">[</span><span class="o">-</span><span class="n">N1</span><span class="p">:</span><span class="o">-</span><span class="n">N2</span><span class="p">]</span><span class="o">.</span><span class="n">all</span><span class="p">()</span></div>


<div class="viewcode-block" id="STD"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAIndicator.html#QUANTAXIS.QAIndicator.base.STD">[docs]</a><span class="k">def</span> <span class="nf">STD</span><span class="p">(</span><span class="n">Series</span><span class="p">,</span> <span class="n">N</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="o">.</span><span class="n">rolling</span><span class="p">(</span><span class="n">Series</span><span class="p">,</span> <span class="n">N</span><span class="p">)</span><span class="o">.</span><span class="n">std</span><span class="p">()</span></div>


<div class="viewcode-block" id="AVEDEV"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAIndicator.html#QUANTAXIS.QAIndicator.base.AVEDEV">[docs]</a><span class="k">def</span> <span class="nf">AVEDEV</span><span class="p">(</span><span class="n">Series</span><span class="p">,</span> <span class="n">N</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    平均绝对偏差 mean absolute deviation</span>
<span class="sd">    修正: 2018-05-25 </span>

<span class="sd">    之前用mad的计算模式依然返回的是单值</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">Series</span><span class="o">.</span><span class="n">rolling</span><span class="p">(</span><span class="n">N</span><span class="p">)</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">x</span> <span class="o">-</span> <span class="n">x</span><span class="o">.</span><span class="n">mean</span><span class="p">()))</span><span class="o">.</span><span class="n">mean</span><span class="p">(),</span> <span class="n">raw</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span></div>


<div class="viewcode-block" id="MACD"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAIndicator.html#QUANTAXIS.QAIndicator.base.MACD">[docs]</a><span class="k">def</span> <span class="nf">MACD</span><span class="p">(</span><span class="n">Series</span><span class="p">,</span> <span class="n">FAST</span><span class="p">,</span> <span class="n">SLOW</span><span class="p">,</span> <span class="n">MID</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;macd指标 仅适用于Series</span>
<span class="sd">    对于DATAFRAME的应用请使用QA_indicator_macd</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">EMAFAST</span> <span class="o">=</span> <span class="n">EMA</span><span class="p">(</span><span class="n">Series</span><span class="p">,</span> <span class="n">FAST</span><span class="p">)</span>
    <span class="n">EMASLOW</span> <span class="o">=</span> <span class="n">EMA</span><span class="p">(</span><span class="n">Series</span><span class="p">,</span> <span class="n">SLOW</span><span class="p">)</span>
    <span class="n">DIFF</span> <span class="o">=</span> <span class="n">EMAFAST</span> <span class="o">-</span> <span class="n">EMASLOW</span>
    <span class="n">DEA</span> <span class="o">=</span> <span class="n">EMA</span><span class="p">(</span><span class="n">DIFF</span><span class="p">,</span> <span class="n">MID</span><span class="p">)</span>
    <span class="n">MACD</span> <span class="o">=</span> <span class="p">(</span><span class="n">DIFF</span> <span class="o">-</span> <span class="n">DEA</span><span class="p">)</span> <span class="o">*</span> <span class="mi">2</span>
    <span class="n">DICT</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;DIFF&#39;</span><span class="p">:</span> <span class="n">DIFF</span><span class="p">,</span> <span class="s1">&#39;DEA&#39;</span><span class="p">:</span> <span class="n">DEA</span><span class="p">,</span> <span class="s1">&#39;MACD&#39;</span><span class="p">:</span> <span class="n">MACD</span><span class="p">}</span>
    <span class="n">VAR</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">DICT</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">VAR</span></div>


<div class="viewcode-block" id="BBIBOLL"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAIndicator.html#QUANTAXIS.QAIndicator.base.BBIBOLL">[docs]</a><span class="k">def</span> <span class="nf">BBIBOLL</span><span class="p">(</span><span class="n">Series</span><span class="p">,</span> <span class="n">N1</span><span class="p">,</span> <span class="n">N2</span><span class="p">,</span> <span class="n">N3</span><span class="p">,</span> <span class="n">N4</span><span class="p">,</span> <span class="n">N</span><span class="p">,</span> <span class="n">M</span><span class="p">):</span>  <span class="c1"># 多空布林线</span>

    <span class="n">bbiboll</span> <span class="o">=</span> <span class="n">BBI</span><span class="p">(</span><span class="n">Series</span><span class="p">,</span> <span class="n">N1</span><span class="p">,</span> <span class="n">N2</span><span class="p">,</span> <span class="n">N3</span><span class="p">,</span> <span class="n">N4</span><span class="p">)</span>
    <span class="n">UPER</span> <span class="o">=</span> <span class="n">bbiboll</span> <span class="o">+</span> <span class="n">M</span> <span class="o">*</span> <span class="n">STD</span><span class="p">(</span><span class="n">bbiboll</span><span class="p">,</span> <span class="n">N</span><span class="p">)</span>
    <span class="n">DOWN</span> <span class="o">=</span> <span class="n">bbiboll</span> <span class="o">-</span> <span class="n">M</span> <span class="o">*</span> <span class="n">STD</span><span class="p">(</span><span class="n">bbiboll</span><span class="p">,</span> <span class="n">N</span><span class="p">)</span>
    <span class="n">DICT</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;BBIBOLL&#39;</span><span class="p">:</span> <span class="n">bbiboll</span><span class="p">,</span> <span class="s1">&#39;UPER&#39;</span><span class="p">:</span> <span class="n">UPER</span><span class="p">,</span> <span class="s1">&#39;DOWN&#39;</span><span class="p">:</span> <span class="n">DOWN</span><span class="p">}</span>
    <span class="n">VAR</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">DICT</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">VAR</span></div>


<div class="viewcode-block" id="BBI"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAIndicator.html#QUANTAXIS.QAIndicator.base.BBI">[docs]</a><span class="k">def</span> <span class="nf">BBI</span><span class="p">(</span><span class="n">Series</span><span class="p">,</span> <span class="n">N1</span><span class="p">,</span> <span class="n">N2</span><span class="p">,</span> <span class="n">N3</span><span class="p">,</span> <span class="n">N4</span><span class="p">):</span>
    <span class="s1">&#39;多空指标&#39;</span>

    <span class="n">bbi</span> <span class="o">=</span> <span class="p">(</span><span class="n">MA</span><span class="p">(</span><span class="n">Series</span><span class="p">,</span> <span class="n">N1</span><span class="p">)</span> <span class="o">+</span> <span class="n">MA</span><span class="p">(</span><span class="n">Series</span><span class="p">,</span> <span class="n">N2</span><span class="p">)</span> <span class="o">+</span>
           <span class="n">MA</span><span class="p">(</span><span class="n">Series</span><span class="p">,</span> <span class="n">N3</span><span class="p">)</span> <span class="o">+</span> <span class="n">MA</span><span class="p">(</span><span class="n">Series</span><span class="p">,</span> <span class="n">N4</span><span class="p">))</span> <span class="o">/</span> <span class="mi">4</span>
    <span class="n">DICT</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;BBI&#39;</span><span class="p">:</span> <span class="n">bbi</span><span class="p">}</span>
    <span class="n">VAR</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">DICT</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">VAR</span></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2018, yutiansut.

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  

    <script type="text/javascript">
        var DOCUMENTATION_OPTIONS = {
            URL_ROOT:'../../../',
            VERSION:'1.0.39',
            LANGUAGE:'python',
            COLLAPSE_INDEX:false,
            FILE_SUFFIX:'.html',
            HAS_SOURCE:  true,
            SOURCELINK_SUFFIX: '.txt'
        };
    </script>
      <script type="text/javascript" src="../../../_static/jquery.js"></script>
      <script type="text/javascript" src="../../../_static/underscore.js"></script>
      <script type="text/javascript" src="../../../_static/doctools.js"></script>
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>

  

  
  
    <script type="text/javascript" src="../../../_static/js/theme.js"></script>
  

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>